#!/bin/bash
#descrption: install and configure pxe环境，通过pxe+kickstart自动安装系统
#注意：需要提前挂载号光盘（或者可以把光盘文件拷贝到httpd服务器）
StartIP=192.168.177.50
StopIP=192.168.177.150
NET=192.168.177.0
MASK=255.255.255.0
ROUTERIP=192.168.177.1
DNS1=202.96.209.133
DNS2=114.114.114.114
TFTPIP=192.168.177.10
HTTPIP=192.168.177.10

function init(){
	#最小化安装系统，需要安装如下包
	yum -y install vim net-tools wget tree gcc gcc-c++ openssh-clients autoconf make lrzsz ntpdate
	#配置epel源
        cd /etc/yum.repos.d/
        [ ! -f epel.repo ] && yum -y install https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
	#关闭防火墙
	systemctl disable firewalld
	systemctl stop firewalld
	sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
        setenforce 0
        #配置时间同步
        /sbin/ntpdate time1.aliyun.com
}
function dhcp(){
	#安装dhcp服务
	yum -y install dhcp
	#配置dhcp服务
	cat >> /etc/dhcp/dhcpd.conf <<EDF
	# option definitions common to all supported networks...
	option domain-name "51yuki.cn";
	option domain-name-servers 202.96.209.133,114.114.114.114;
	default-lease-time 600;
	max-lease-time 7200;

	log-facility local7;
	subnet $NET netmask $MASK {
		range $StartIP $StopIP;
		option subnet-mask $MASK;
		option routers $ROUTERIP;
		option domain-name-servers $DNS1,$DNS2;
		default-lease-time 21600;
		max-lease-time 43200;
		next-server $TFTPIP ;
		filename "pxelinux.0";
	}
EDF

#启动dhcp服务
systemctl enable dhcpd
systemctl start dhcpd
	if [ "`ss -tunlp|grep 67|awk -F: '{print $2}'|awk '{print $1}'`" == "67" ];then
		echo "dhcp server start succfull"
	else
		echo "dhcp server start fail"
		exit 1
	fi
}


function tftp(){
	#安装tftp服务
	yum install tftp-server -y
	#安装syslinux
	yum -y install syslinux
	cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
	#启动tftp服务
	systemctl enable tftp
	systemctl start tftp
}



function http(){
	#install httpd service
	yum -y install httpd
	#挂载光盘到网站根目录下的某个文件中
	mkdir /var/www/html/centos7.4
	mount /dev/cdrom/ /var/www/html/centos7.4
	cp -a /var/www/html/centos7.4/isolinux/* /var/lib/tftpboot/
	\cp /var/www/html/centos7.4/pxeboot/initrd.img /var/lib/tftpboot/
	\cp /var/www/html/centos7.4/pxeboot/vmlinuz /var/lib/tftpboot/
	mkdir -p /var/lib/tftpboot/pxelinux.cfg
	#配置default文件
	cp /var/www/html/centos7.4/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
       #
	cd /var/lib/tftpboot/pxelinux.cfg/
	startline=$(sed -n "/\^Install CentOS 7/=" default)
	lineafter=2
	let endline="$startline + $lineafter"
	sed -i "$startline,$endline d" default
	sed -i "/label linux/a   menu label ^Install CentOS 7\n  kernel vmlinuz\n append initrd=initrd.img ks=http://$HTTPIP/centos7.cfg net.ifnames=0 biosdevname=0" default
	#配置ks文件
	cd /var/www/html
	cat >centos7.cfg <<EOF
	#version=DEVEL
#Install OS instead of upgrade
install
# X Windows system configuration information
xconfig --startxonboot
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
#Reboot after installation
reboot
# System authorization information
auth --enableshadow --passalgo=sha512
# Root password
rootpw --iscrypted \$6\$mvLkrJbn.KQCw.Ev\$KjLKoHjWoqG1mmDdMd73MiCIe4Z8lDK42n.rHLLLYkoINqaXUJzkGGJFzvXp8.JdTaEPjXY1NZIIj4tw8WQn80
#create user admin
user --name=admin --password=\$6\$SZtk92AfcisRCCYE\$Oli2yz8zhGNCpHmD2GAqXYFd50kflw7AzVn8Xr5RW9kSi6pP9oS1OKKdrvXKhhmEeXqDfAuMZ3A0h6nYF62kg/ --iscrypted --gecos="admin"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#Use network install
url --url="http://${HTTPIP}/centos7.4/"
# System language
lang en_US.UTF-8
#Use graphical install
text
#firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --activate
# Use test mode install
firstboot --disable
#SElinux configuration
selinux --permissive
# System bootloader configuration
ignoredisk --only-use=sda
zerombr
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
clearpart --all --initlabel
#Disk partitioning information
#autopart --type=lvm
part /boot --fstype xfs --size 1024   
part swap --size 4096   
part / --fstype xfs --size 1 --grow

%packages
@^minimal
@core
@development
@compat-libraries
kexec-tools
gcc
gcc-c++
autoconf
make
openssh-clients
sysstat
tree
lrzsz
dos2unix
wget
net-tools
vim
curl
bash-completion
%end
%post 
	systemctl disable postfix.service  
%end
EOF
	#重启服务
		sync
		systemctl enable httpd
		systemctl start httpd
		systemctl restart tftp
		systemctl restart dhcpd
}
DATE=$(date +%F)
KERNEL_VERSION=$(uname -r)
HOSTNAME=$(hostname)	
cat <<EOF
-----------------------------------------------------------------
                auto install and configure  PXE+KICKSTART
----------------------------------------------------------------
| DATE                   :  $DATE
| IP                     :  $HTTPIP
| KERNEL_VERSION         :  ${KERNEL_VERSION}
| HOSTNAME               :  $HOSTNAME
---------------------------------------------------------------
|*********Please Enter your Choice:***************************|
(1) Initialize the environment
(2) install and configure dhcp service
(3) install and configure tftp service
(4) install and configure httpd service
(5) quit
EOF
read -p "please enter your choice:" input
case $input in
1)
   init
   ;;
2)
   dhcp
   ;;
3)
  tftp
  ;;
4)
  http
  ;;
5)
  exit 5
esac
